﻿using UnityEngine;
using System.Collections;

public class HighScoreScript : MonoBehaviour {
	//bool userHasHitOk = false;
	public int maxNumberOfScores = 6;
	private GUIScript _gui;
	public bool isHighScore = false;
	
	

	// Use this for initialization
	void Awake ()
	{
		//DontDestroyOnLoad(this.gameObject);
		AddScore("",0);
	}
	
	public bool IsHighScore(int _newScore)
	{
		// We only need to check if the new score is greater than the lowest one if has already been filled
		if(PlayerPrefs.HasKey(maxNumberOfScores-1+"HScore"))
			if( _newScore > PlayerPrefs.GetInt(maxNumberOfScores-1+"HScore"))
				return true;
			else
				return false;
		return true;
	}

    
	public void AddScore(string newName, int newScore)
	{
		int oldScore;
		string oldName;
		
		for(int i=0;i<maxNumberOfScores;i++)
		{
			if(PlayerPrefs.HasKey(i+"HScore"))
			{
				if(PlayerPrefs.GetInt(i+"HScore")<newScore)
				{
					// new score is higher than the stored score
					oldScore = PlayerPrefs.GetInt(i+"HScore");
					oldName = PlayerPrefs.GetString(i+"HScoreName");
					PlayerPrefs.SetInt(i+"HScore",newScore);
					PlayerPrefs.SetString(i+"HScoreName",newName);
					newScore = oldScore;
					newName = oldName;
				}
			}
			else
			{
				PlayerPrefs.SetInt(i+"HScore",newScore);
				PlayerPrefs.SetString(i+"HScoreName",newName);
				newScore = 0;
				newName = "";
			}
		}
	}
	
	public void ResetScores()
	{
		for(int i=0;i<maxNumberOfScores;i++)
		{
		
			PlayerPrefs.SetInt(i+"HScore",0);
			PlayerPrefs.SetString(i+"HScoreName","");
		}
	}


}
